﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.IO;
using Scheme.Net.Runtime.AST;

namespace Scheme.Net.Runtime
{
    public class Port : IEvaluable
    {
        private TextReader reader;
        private TextWriter writer;

        public Port(TextReader reader) 
        { 
            this.reader = reader;
            Type = PortType.Input;
        }

        public Port(TextWriter writer) 
        { 
            this.writer = writer;
            Type = PortType.Output;
        }

        public PortType Type { get; private set; }

        public override string ToString()
        {
            return "#<i/o port>";
        }

        public void Close()
        {
            if (Type == PortType.Input)
                reader.Close();
            else
                writer.Close();
        }

        public TextReader Reader
        {
            get
            {
                if (Type != PortType.Input)
                    throw new SchemeRuntimeException("Not an input port.");
                return reader;
            }
        }

        public TextWriter Writer
        {
            get
            {
                if (Type != PortType.Output)
                    throw new SchemeRuntimeException("Not an output port.");
                return writer;
            }
        }
    }

    public enum PortType
    {
        Input,
        Output
    }
}
